//!!!!!!!!!此处不该这样写，只是临时方案。参照纯函数编程以及ES6的Promise

//封装的网络请求方法
var wxurl = 'https://ruianm.tour-ma.com';
// var wxurl = 'https://2474og0368.zicp.vip/'
var wxtitle = '';


//登录微信
// #ifdef APP-PLUS
function loginUser(callbs, callbf) {
	uni.login({ //1.请求微信登录
		provider: 'weixin',
		success: function(res) {},
		fail() {
			var error = {
				msg: '用户拒绝授权，登录失败！wx',
				errormsg: e
			}
			uni.showModal({
				content: error,
				showCancel: false,
				cancelText: '',
				confirmText: '确定',
			});
		}
	})
}
// #endif

// #ifndef APP-PLUS
function loginUser(callbs, callbf) {
	uni.showToast({
		title: '加载中',
		icon: 'loading',
		mask: true,
		duration: 10000
	});
	uni.login({ //1.请求微信登录
		provider: 'weixin',
		success: function(res) {
			//通过微信code，从服务器请求session
			var url = '/minapps/login.jspx';
			var code = res.code;
			uni.request({
				url: wxurl + url, //拿code请求服务器
				data: {
					code: code,
					type: 2
				},
				success: function(res) {
					uni.hideToast();
					if (res.data.status == 200) {
						typeof callbs == "function" && callbs(res); //获取key成功
						//getUser();          //回调微信用户
					} else if (res.data.status == 2002) {
						//无用户状态，跳转绑定页面
					} else {
						var error = {
							msg: '与服务器换取session失败，请稍后重试！jy',
							errormsg: res
						}
						typeof callbf == "function" && callbf(error); //获取key失败
					}
				},
				fail: function(e) {
					uni.hideToast();
					var error = {
						msg: '与服务器换取session失败。请稍后重试！jy',
						errormsg: e
					}
					typeof callbf == "function" && callbf(error); //获取key失败
				}
			})
			//通过微信code，从服务器请求session。 end//
		}
	})
}
// #endif 
function httpPostai(that, murl, mpara, callbs, callbf) {
	uni.showToast({
		title: '加载中',
		icon: 'loading',
		mask: true,
		duration: 10000
	});
	// #ifdef MP-WEIXIN
	mpara.wx = 'wx';
	// #endif
	uni.request({
		url: murl, //用户信息交换接口
		method: 'POST',
		data: JSON.stringify(mpara),
		header: {
			"Content-Type": "text/html;charset=UTF-8"
		},
		success: function(res) {
			uni.hideToast();
			typeof callbs == "function" && callbs(res);
		},
		fail: function(e) {
			uni.hideToast();
			var error = {
				msg: '获取数据失败，请稍后重试！jy',
				errormsg: e
			}
			typeof callbf == "function" && callbf(error);
		}
	});
}
//post请求，参数分别为：当前页面、url、参数对象、成功回调、失败回调
function httpPost(that, murl, mpara, callbs, callbf) {
	var pages = getCurrentPages();
	var page = pages[pages.length - 1].route;
	//检查用户登录状态获取key，参数分别为：成功回调、失败回调
	//checkUser(function(key) {
	//此时key应该有值，无值说明用户未给权限
	var key = '';
	try {
		key = uni.getStorageSync('session');
	} catch (e) {
		console.log(e);
	}
	// #ifdef MP-WEIXIN
	mpara.wx = 'wx';
	// #endif
	uni.request({
		url: wxurl + murl, //用户信息交换接口
		method: 'POST',
		data: JSON.stringify(mpara),
		header: {
			"Content-Type": "text/html;charset=UTF-8",
			'Cookie': 'WXSESSIONID=' + key
		},
		success: function(res) {
			if (res.data.status == 200) {
				typeof callbs == "function" && callbs(res);
			} else if (res.data.status == 2001) {
				uni.removeStorageSync('session')
				if (page == 'pages/wode/wode' || page == 'pages/index/index') {
					typeof callbs == "function" && callbs(res);
					return
				}
				//未登录状态
				try {
					uni.showModal({
						title: '',
						content: '需要登录,是否登录',
						showCancel: true,
						cancelText: '取消',
						confirmText: '登录',
						success: res => {
							if (res.confirm) {
								uni.navigateTo({
									url: '/pages/denglu/denglu'
								})
							} else {
								uni.navigateBack({
									delta: 1
								})
							}
						},
						fail: () => {},
						complete: () => {}
					});
					//httpPost(that, murl, mpara, callbs, callbf);
				} catch (e) {
					var error = {
						msg: '删除KEY失败，请稍后重试！wx',
						errormsg: e
					}
				}
				typeof callbs == "function" && callbs(res);
			} else if (res.data.status == 2002) {
				//无用户状态，跳转绑定页面
				//uni.redirectTo({ url: "/pages/user/mobile/mobile" });
			} else {
				var error = {
					msg: 'jy出错：' + res.data.message,
					errormsg: res
				}
				typeof callbs == "function" && callbs(res);
			}
		},
		fail: function(e) {
			uni.hideToast();
			var error = {
				msg: '获取数据失败，请稍后重试！jy',
				errormsg: e
			}
			typeof callbf == "function" && callbf(error);
		}
	});
	// }, function(e) {
	// 	typeof callbf == "function" && callbf(e); //接收到的callbf，继续抛出
	// });
}

//post请求，参数分别为：当前页面、url、参数对象、成功回调、失败回调
function httpGet(that, murl, mpara, callbs, callbf) {
	uni.showToast({
		title: '加载中',
		icon: 'loading',
		mask: true,
		duration: 10000
	});
	var key = '';
	try {
		key = uni.getStorageSync('session');
	} catch (e) {
		console.log(e);
	}
	// #ifndef APP-PLUS
	mpara.wx = 'wx';
	// #endif
	uni.request({
		url: wxurl + murl, //用户信息交换接口
		method: 'POST',
		data: JSON.stringify(mpara),
		header: {
			"Content-Type": "text/html;charset=UTF-8",
			'Cookie': 'WXSESSIONID=' + key
		},
		success: function(res) {
			uni.hideToast()
			if (res.data.status == 200) {
				typeof callbs == "function" && callbs(res);
			} else {
				var error = {
					msg: 'jy出错：' + res.data.message,
					errormsg: res
				}
				typeof callbs == "function" && callbs(res);
			}
		},
		fail: function(e) {
			uni.hideToast()
			var error = {
				msg: '获取数据失败，请稍后重试！jy',
				errormsg: e
			}
			typeof callbf == "function" && callbf(error);
		}
	});
}

function httpGetg(that, murl, mpara, callbs, callbf) {
	var url = wxurl + murl;
	var len = 0;
	for (var key in mpara) {
		if (len == 0) {
			url += '?' + key + '=' + mpara[key];
		} else {
			url += '&' + key + '=' + mpara[key];
		}
		len++
	}
	uni.request({
		url: url, //用户信息交换接口
		method: 'GET',
		header: {
			"Content-Type": "text/html;charset=UTF-8"
		},
		success: function(res) {
			if (res.data.status == 200) {
				typeof callbs == "function" && callbs(res);
			} else {
				var error = {
					msg: 'jy出错：' + res.data.message,
					errormsg: res
				}
				typeof callbs == "function" && callbs(res);
			}
		},
		fail: function(e) {
			var error = {
				msg: '获取数据失败，请稍后重试！jy',
				errormsg: e
			}
			typeof callbf == "function" && callbf(error);
		}
	});
}

function httpGejqr(that, murl, mpara, callbs, callbf) {
	var url = wxurl + murl;
	uni.request({
		url: url, //用户信息交换接口
		method: 'GET',
		header: {
			"Content-Type": "text/html;charset=UTF-8"
		},
		success: function(res) {
			if (res.data.status == 200) {
				typeof callbs == "function" && callbs(res);
			} else {
				var error = {
					msg: 'jy出错：' + res.data.message,
					errormsg: res
				}
				typeof callbs == "function" && callbs(res);
			}
		},
		fail: function(e) {
			var error = {
				msg: '获取数据失败，请稍后重试！jy',
				errormsg: e
			}
			typeof callbf == "function" && callbf(error);
		}
	});
}

function getWeek(date) {
	var week;
	if (date.getDay() == 0) week = "周日"
	if (date.getDay() == 1) week = "周一"
	if (date.getDay() == 2) week = "周二"
	if (date.getDay() == 3) week = "周三"
	if (date.getDay() == 4) week = "周四"
	if (date.getDay() == 5) week = "周五"
	if (date.getDay() == 6) week = "周六"
	return week;
}

function iskong(name) {
	if (name == '') {
		return true
	}
}
module.exports = {
	httpGetg: httpGetg,
	loginUser: loginUser,
	httpPost: httpPost,
	httpGet: httpGet,
	httpPostai: httpPostai,
	httpGejqr: httpGejqr,
	getWeek: getWeek,
	iskong: iskong
}
